package com.tuling.service;

import com.tuling.dao.CartDao;
import com.tuling.dao.CartDaoImpl;
import com.tuling.dao.FruitDao;
import com.tuling.dao.FruitDaoImpl;
import com.tuling.domain.Cart;
import com.tuling.domain.Fruit;

import java.util.ArrayList;
import java.util.List;

public class CartServiceImpl implements CartService{
    private CartDao cartDao = new CartDaoImpl();
    private FruitDao fruitDao = new FruitDaoImpl();
    @Override
    public int add(int uid, Cart cart) {
        return cartDao.add(uid, cart);
    }

    @Override
    public Cart find(int uid, int fid) {
        return cartDao.find(uid, fid);
    }

    @Override
    public int update(int uid, Cart cart) {
        return cartDao.update(uid, cart);
    }

    @Override
    public List<Cart> findByUid(int uid) {
        return cartDao.findByUid(uid);
    }

    @Override
    public List<Fruit> show(int uid, boolean flag) {
        List<Cart> carts = findByUid(uid);
        ArrayList<Fruit> fruits = new ArrayList<>();
        if (flag) {
            // 显示购物车列表
            for (Cart cart : carts) {
                if (cart.getIsCart()) {
                    Fruit fruit = fruitDao.getById(cart.getFid());
                    fruits.add(fruit);
                }
            }
        } else {
            // 显示关注列表
            for (Cart cart : carts) {
                if (cart.getIsStar()) {
                    Fruit fruit = fruitDao.getById(cart.getFid());
                    fruits.add(fruit);
                }
            }
        }
        return fruits;
    }

    @Override
    public int del(int uid, int fid) {
        return cartDao.del(uid, fid);
    }
}
